<template>
  <div class="user-wrapper" :class="theme">

   <span class="action" @click="showClick">
      <a-icon type="search"></a-icon>
    </span>
    <component :is="searchMenuComp" v-show="searchMenuVisible || isMobile()" class="borders"
               :visible="searchMenuVisible" title="搜索菜单" :footer="null" @cancel="searchMenuVisible=false">
      <a-select
        class="search-input"
        v-model="menuSearch"
        showSearch
        :allowClear="false"
        :showArrow="false"
        placeholder="搜索菜单"
        optionFilterProp="children"
        :filterOption="filterOption"
        :open="isMobile()?true:null"
        :getPopupContainer="(node) => node.parentNode"
        :style="isMobile()?{width: '100%',marginBottom:'50px'}:{}"
        @change="searchMethods"
        @blur="hiddenClick">
        <a-select-option v-for="(site,index) in searchMenuOptions" :key="index" :value="site.id">{{ site.meta.title }}
        </a-select-option>
      </a-select>
    </component>

    <a-dropdown>
      <span class="action">
        <a class="logout_title">
          <a-icon type="qrcode"></a-icon>
        </a>
      </span>
      <a-menu slot="overlay">
        <a-menu-item key="1">
          <a @click="handleAndroidClick" style="display: inline-block">
            <a-icon type="android"/>
            Android</a>
        </a-menu-item>
        <a-menu-item key="2">
          <a @click="handleIosClick" style="display: inline-block">
            <a-icon type="apple"/>
            iOS</a>
        </a-menu-item>
      </a-menu>
    </a-dropdown>

    <header-notice class="action"/>
    <a-dropdown>
      <span class="action action-full ant-dropdown-link user-dropdown-menu">
        <a-avatar class="avatar" v-if="avatar()" size="small" :src="getAvatar()"/>
        <a-avatar class="avatar" v-else size="small" icon="user"/>
        <span v-if="isDesktop()">欢迎您，{{ nickname() }}</span>
      </span>
      <a-menu slot="overlay" class="user-dropdown-menu-wrapper">
        <!--<a-menu-item key="0">-->
        <!--<router-link :to="{ name: 'account-center' }">-->
        <!--<a-icon type="user"/>-->
        <!--<span>个人中心</span>-->
        <!--</router-link>-->
        <!--</a-menu-item>-->
        <!--<a-menu-item key="1">-->
        <!--<router-link :to="{ name: 'account-settings-base' }">-->
        <!--<a-icon type="setting"/>-->
        <!--<span>账户设置</span>-->
        <!--</router-link>-->
        <!--</a-menu-item>-->
        <a-menu-item key="2" @click="updatePassword">
          <a-icon type="setting"/>
          <span>密码修改</span>
        </a-menu-item>
        <!-- <a-menu-item key="2" disabled>
           <a-icon type="setting"/>
           <span>测试</span>
         </a-menu-item>
         <a-menu-divider/>
         <a-menu-item key="3">
           <a href="javascript:;" @click="handleLogout">
             <a-icon type="logout"/>
             <span>退出登录</span>
           </a>
         </a-menu-item>-->
      </a-menu>
    </a-dropdown>
    <span class="action">
      <a class="logout_title" href="javascript:;" @click="handleLogout">
        <a-icon type="logout"/>
        <span v-if="isDesktop()">&nbsp;退出登录</span>
      </a>
    </span>
    <user-password ref="userPassword"></user-password>
    <qrcode-modal ref="QrcodeModalForm"></qrcode-modal>
  </div>
</template>

<script>
import HeaderNotice from './HeaderNotice'
import UserPassword from './UserPassword'
import {mapActions, mapGetters, mapState} from 'vuex'
import {mixinDevice} from '@/utils/mixin.js'
import QrcodeModal from "./qrcodeModal";
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'

export default {
  name: "UserMenu",
  mixins: [mixinDevice],
  components: {
    QrcodeModal,
    HeaderNotice,
    UserPassword,
  },
  props: {
    theme: {
      type: String,
      required: false,
      default: 'dark'
    }
  },
  data() {
    return {
      searchMenuOptions: [],
      searchMenuComp: 'span',
      searchMenuVisible: false,
      menuSearch: undefined,   //设置成undefined是想展示出来placeholder
      projectName:window._CONFIG['projectName']
    }
  },
  created() {
    let lists = []
    this.searchMenus(lists, this.permissionMenuList)
    this.searchMenuOptions = [...lists]
  },
  computed: {
    ...mapState({
      // 后台菜单
      permissionMenuList: state => state.user.permissionList
    })
  },
  watch: {
    device: {
      immediate: true,
      handler() {
        this.searchMenuVisible = false
        this.searchMenuComp = this.isMobile() ? 'a-modal' : 'span'
      },
    },
  },
  methods: {
    ...mapActions(["Logout"]),
    ...mapGetters(["nickname", "avatar", "userInfo"]),
    getAvatar() {
      // console.log('url = '+ window._CONFIG['imgDomainURL']+"/"+this.avatar())
      return window._CONFIG['imgDomainURL'] + "/" + this.avatar()
    },
    handleLogout() {
      const that = this
      this.$confirm({
        title: '提示',
        content: '真的要注销登录吗 ?',
        onOk() {
          return that.Logout({}).then(() => {
            window.location.href = "/";
          }).catch(err => {
            that.$message.error({
              title: '错误',
              description: err.message
            })
          })
        },
        onCancel() {
        },
      });
    },
    updatePassword() {
      let username = this.userInfo().username
      this.$refs.userPassword.show(username)
    },
    /*安卓二维码*/
    handleAndroidClick() {
      this.$refs.QrcodeModalForm.edit('Android')
      this.$refs.QrcodeModalForm.title = 'Android'
    },
    /*下载iOS码*/
    handleIosClick() {
      var that = this
      this.$confirm({
        title: '兑换码有限，请按需下载',
        content: '是否确认下载综合管理平台iOS版本APP?',
        onOk: function () {
          that.$refs.QrcodeModalForm.edit('iOS')
          that.$refs.QrcodeModalForm.title = 'iOS'
        }
      })
    },
    showClick() {
      this.searchMenuVisible = true
      this.menuSearch = undefined
    },
    goOtherSystem(){
      let token=Vue.ls.get(ACCESS_TOKEN)
      window.open(window._CONFIG['anotherSystemBrowserURL']+'?token='+token)
      // window.open('http://172.17.17.6:3000?token='+token)
    },
    hiddenClick() {
      // this.shows = false
    },
    searchMenus(arr, menus) {
      for (let i of menus) {
        if (!i.hidden && "layouts/RouteView" !== i.component) {
          arr.push(i)
        }
        if (i.children && i.children.length > 0) {
          this.searchMenus(arr, i.children)
        }
      }
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    searchMethods(value) {
      let route = this.searchMenuOptions.filter(item => item.id === value)[0]
      if (route.meta.internalOrExternal === true) {
        window.open(route.meta.url, '_blank')
      } else {
        if (route.component.includes('layouts/IframePageView')) {
          this.$router.push(route)
        } else {
          this.$router.push({path: route.path})
        }
      }
      this.searchMenuVisible = false
    },
  }
}
</script>

<style scoped lang="less">
.logout_title {
  color: inherit;
  text-decoration: none;
}

.tip {
  width: 100%;
  text-align: center;
  padding: 10px;
  color: #f5222d;
  font-size: 16px;
  font-weight: bold;
}

.tip:before {
  display: inline-block;
  margin-right: 3px;
  color: #f5222d;
  font-size: 14px;
  font-family: SimSun, sans-serif;
  content: "*";
}

.user-wrapper .search-input {
  width: 180px;
  color: inherit;

  /deep/ .ant-select-selection {
    background-color: inherit;
    border: 0;
    border-bottom: 1px solid white;

    &__placeholder, &__field__placeholder {
      color: inherit;
    }
  }
}
</style>